package org.tau.workshop2011.semantics

import org.tau.workshop2011.parsing.AST._

object DependencyResolver extends ASTDeepVisitor {
  def resolveDependencies(widget: Widget): DependencyTracker[ResolvedVariable] = {
    val dt: DependencyTracker[ResolvedVariable] = new DependencyTracker[ResolvedVariable]
    val GE = widget.environment.getGlobalSymbolTable

    for (gName <- GE.globalNames) {
      val rv = GE.globalGet(gName)
      dt.introduceComponent(rv)
      rv.definition.value match {
        case Some(value) => value accept (new ASTDeepVisitor {
          override def visit(node: Variable): Any = {
            dt.addDependency(rv, node.environment.get(node))
          }
        })
        case None =>
      }
    }

    dt.expand() 
    dt
  }
}